{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# kNN 基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "raw_data_X = [\n",
    "    [3.393533211, 2.331273381],\n",
    "    [3.110073483, 1.781539638],\n",
    "    [1.343808831, 3.368360954],\n",
    "    [3.582294042, 4.679179110],\n",
    "    [2.280362439, 2.866990263],\n",
    "    [7.423436942, 4.696522875],\n",
    "    [5.745051997, 3.533989803],\n",
    "    [9.172168622, 2.511101045],\n",
    "    [7.792783481, 3.424088941],\n",
    "    [7.939820817, 0.791637231]\n",
    "]  # 两列分别代表肿瘤的长和宽\n",
    "raw_data_y = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] # 0是良性肿瘤，1是恶性肿瘤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X_train = np.array(raw_data_X)\n",
    "y_train = np.array(raw_data_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3.39353321,  2.33127338],\n",
       "       [ 3.11007348,  1.78153964],\n",
       "       [ 1.34380883,  3.36836095],\n",
       "       [ 3.58229404,  4.67917911],\n",
       "       [ 2.28036244,  2.86699026],\n",
       "       [ 7.42343694,  4.69652288],\n",
       "       [ 5.745052  ,  3.5339898 ],\n",
       "       [ 9.17216862,  2.51110105],\n",
       "       [ 7.79278348,  3.42408894],\n",
       "       [ 7.93982082,  0.79163723]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAExlJREFUeJzt3WGMXfWd3vHvszBVEns3VOtZh2KMK0CrjaMU6JhQaKMR\naapAUKhWvACLmPDGJUIr0q6ImkibyOZVpVUUARIWgtRByRKZkI0iMKtNFewQrSAZO44DdlTR1qxh\n2XiSdIe4RFk7+fXFPYZhdsZzZ+YO586Z70c6uufc+597Ho3sZ/7zv+fOTVUhSeqW32k7gCRp8Cx3\nSeogy12SOshyl6QOstwlqYMsd0nqIMtdkjrIcpekDrLcJamDzm3rxOvWratNmza1dXpJWpEOHDjw\ns6oanW9ca+W+adMmJiYm2jq9JK1ISV7qZ5zLMpLUQZa7JHWQ5S5JHWS5S1IHWe6S1EGWuyR1kOUu\nSR1kuUtaPlNTsHlz71ZvK8tdbxjfPc747vG2Y6hLnnwSjhyBvXvbTrLqWO6SBm/rVli7Fm67rXe8\nbVvveOvWdnOtIq39+QENjzOz9f0v7X/L8b5P7GsnkFa+nTvh0CE4dgxOn4aREbjoIrjnnraTrRrO\n3CUN3iWX9Ar+1ClYs6Z3u2MHXHxx28lWDWfuemOG7oxdA7VnT6/Y/+zPejP2xx6Dm25qO9WqYblL\nWh533w333Qfr18Ott8Lx420nWlUsd73BGbsGasuWN/fXr+9tetu45i5JHWS5S1IH9V3uSc5J8sMk\nT8zy2HiSqSSHmu1zg40pSVqIhay53wUc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P+K/tRnpTs0z0MWBoPo06yT8HbqT3g/FfAGuS3Npuqp6qOgr8\nN+Cv6S3JHAJ+00YWy30OzZr248BXq+obbeeZqfnV/WngI21naVwDfKxZ2/4acG2Sr7Qb6U3NbI+q\nOgH8Jb010WHwMvDytN/Avk6v7IfFdcDBqvpp20Gm+ffA/6mqyao6BXwDuLrlTG+oqoer6l9X1QeB\n/wv8zzZyWO6zaF64fBg4WlVfaDvPGUlGk5zX7L8T+DDwk3ZT9VTVZ6pqQ1Vtovdr/HeqaihmU0nW\nNC+M0yx5/Ad6vz63rqr+Hjie5A+buz4EtPrC/Qy3MERLMo2/Ba5K8q7m/+qH6L0uNhSS/EFzu5He\nevtftJHj3DZOCpDkUWAcWJfkZeDzVfVwW3lmuAb4OPDjZn0b4LNVtbfFTADnA19url74HWBPVQ3V\nJYdDaj3wl70e4FzgL6rqr9qN9BZ/Any1WQL538DtLecB3vhB+GHgP7WdZbqqei7J14GDwGnghwzB\nO0KneTzJ7wOngDvbeoHcd6hKUge5LCNJHWS5S1IHWe6S1EGWuyR1kOUuSR1kuUtSB1nuktRBlrsk\nddD/B4wCfjs5F3jVAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x116831a58>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X_train[y_train==0,0], X_train[y_train==0,1], color='g', marker=\"+\")\n",
    "plt.scatter(X_train[y_train==1,0], X_train[y_train==1,1], color='r', marker=\"*\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x = np.array([8.093607318, 3.365731514])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACx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f4\n723msdxP0rxo+TBwsKq+2Hae6ZKMJjm/2X8X8GHgx+2mgqr6bFWtqqq19H6N/3ZVtT6bAkiyrHlh\nnGbJ49/S+xW6dVX1f4DDSf6wuetDQOsv3E9zK0O0JNP4CXB1kvOa/6sfove6WOuS/OPmdjW99fa/\nbDPPuW2dOMmjwDiwIskrwBeq6uG28kxzLfAJ4EfN2jbA56pqZ4uZplwAfKW5guEdwI6qGqrLDofQ\nSuCvej3AucBfVtVftxvpbf4DsL1ZAvmfwO0t5wHe/EH4YeDft51luqp6PsnjwF7gBPADhuQdocAT\nSf4AOA7c2faL475DVZI6yGUZSeogy12SOshyl6QOstwlqYMsd0nqIMtdkjrIcpekDrLcJamD/j9X\nFKjGBztfpQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x119ba7f98>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(x[0], x[1], color=\"blue\", marker=\"o\")\n",
    "plt.scatter(X_train[y_train==0,0], X_train[y_train==0,1], color='g', marker=\"+\")\n",
    "plt.scatter(X_train[y_train==1,0], X_train[y_train==1,1], color='r', marker=\"*\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## kNN的过程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4.812566907609877,\n",
       " 5.229270827235305,\n",
       " 6.749798999160064,\n",
       " 4.6986266144110695,\n",
       " 5.83460014556857,\n",
       " 1.4900114024329525,\n",
       " 2.354574897431513,\n",
       " 1.3761132675144652,\n",
       " 0.3064319992975,\n",
       " 2.5786840957478887]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from math import sqrt\n",
    "distances = []\n",
    "for x_train in X_train:\n",
    "    d = sqrt(np.sum((x_train - x)**2))\n",
    "    distances.append(d)\n",
    "distances"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 7, 5, 6, 9, 3, 0, 1, 4, 2])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argsort(distances) # 排序后返回元素的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 7, 5, 6, 9, 3, 0, 1, 4, 2])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nearest = np.argsort(distances) # 排序后返回元素的索引\n",
    "nearest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "k = 6 # K近邻算法，取最近的6个点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "topK_y = [y_train[i] for i in nearest[:k]]  # 取最近的k个点的下表，对应着去y_train中把指定下标的元素取出来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 1, 1, 1, 1, 0]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "topK_y  # 最近的6个点，5个对应的结果都是同一类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({0: 1, 1: 5})"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 下面对topk_y中的元素分布做一下统计\n",
    "from collections import Counter\n",
    "Counter(topK_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "votes = Counter(topK_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1, 5), (0, 1)]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "votes.most_common(2) # 找出票数最多的n个元素，2就是n的具体值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(1, 5)]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "votes.most_common(1) # 找出票数最多的n个元素，1就是n的具体值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "predict_y = votes.most_common(1)[0][0]  # 注意取出具体的预测的值的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predict_y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
